package com.zzl.cwlyxt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzl.cwlyxt.entity.Type;
import com.zzl.cwlyxt.entity.Users;
import com.zzl.cwlyxt.service.UsersService;
import com.zzl.cwlyxt.util.Result;

import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * (Users)表控制层
 *
 * @author makejava
 * @since 2024-03-05 17:17:48
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("users")
public class UsersController {
    /**
     * 服务对象
     */
    @Resource
    private UsersService usersService;

    /**
     * 分页查询
     *
     * @param users 筛选条件
     * @param limit 分页对象
     * @param page  分页对象
     * @return 查询结果
     */
    @GetMapping("findAll")
    public Result findAll(Users users, Integer limit, Integer page) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        return usersService.selectAll(users, limit, page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getById")
    public Result getById(Integer id) {
        return Result.success(this.usersService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param users 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(Users users) {
        //判断是否为空
        if (users == null) {
            return Result.failure("参数不能为空");
        }

        //根据账号查询
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("account", users.getAccount());
        Users one = usersService.getOne(usersQueryWrapper);

        if (one != null) {
            return Result.failure("该账号已存在");
        }

        if (this.usersService.save(users)) {
            return Result.success("添加成功");
        }
        return Result.failure("添加失败");
    }

    /**
     * 编辑数据
     *
     * @param users 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(Users users) {
        //判断是否为空
        if (users == null) {
            return Result.failure("参数不能为空");
        }

        //根据账号查询
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("account", users.getAccount());
        Users one = usersService.getOne(usersQueryWrapper);

        if (one != null && !Objects.equals(one.getUserid(), users.getUserid())) {
            return Result.failure("该账号已存在");
        }

        if (this.usersService.updateById(users)) {
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("delete")
    public Result deleteById(Integer id) {
        //判断id是否为空
        if (id == null) {
            return Result.failure("id不能为空");
        }

        if (this.usersService.removeById(id)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }

    /**
     * 用户登录
     *
     * @param users 用户实体
     * @return 登录结果
     */
    @PostMapping("login")
    public Result login(Users users) {
        //判断是否为空
        if (users == null) {
            return Result.failure("参数不能为空");
        }

        //根据账号进行查找
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("account", users.getAccount());
        Users one = usersService.getOne(usersQueryWrapper);

        if (one == null) {
            return Result.failure("账号不存在,请先注册");
        }

        if (!one.getPassword().equals(users.getPassword())) {
            return Result.failure("密码错误,请重新输入");
        }

        return Result.success(one);
    }

    @PostMapping("register")
    public Result register(Users users) {
        //判断是否为空
        if (users == null) {
            return Result.failure("参数不能为空");
        }

        //根据账号进行查找
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("account", users.getAccount());
        Users one = usersService.getOne(usersQueryWrapper);

        if (one != null) {
            return Result.failure("账号已存在,请重新输入");
        }

        if (this.usersService.save(users)) {
            return Result.success("注册成功");
        }
        return Result.failure("注册失败");
    }
}

